using System;
using System.Xml; 
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
 

public partial class XmlWriterPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
            {
            string fileName = Server.MapPath("XmlWriter.xml");
            XmlWriter xmlWriter = XmlWriter.Create(fileName);
            
            xmlWriter.WriteStartDocument();
            xmlWriter.WriteComment(
                "Written by Pham Huu Khang " 
                + DateTime.Now.ToLongDateString());
            xmlWriter.WriteStartElement("root");
            for(int i=1; i<3; i++)
            {
             
                xmlWriter.WriteStartElement("Item" );
                xmlWriter.WriteAttributeString("ID", "ItemID" + i.ToString());            
                xmlWriter.WriteElementString("Name", "ItemName" + i.ToString());
                xmlWriter.WriteElementString("Unit", "Unit" + i.ToString());
                xmlWriter.WriteElementString("Quantity",  i.ToString());
                xmlWriter.WriteElementString("Price", i.ToString());
                xmlWriter.WriteEndElement();
            }
            xmlWriter.WriteEndElement();
            xmlWriter.WriteEndDocument();
             
            xmlWriter.Flush();         
            xmlWriter.Close();
        }
        catch(Exception ex)
        {
            Literal1.Text = ex.Message;
        }
       
    }
}
